package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.BankListAdapter;
import com.haotunet.app.goldtreasure.model.BankInfo;
import com.haotunet.app.goldtreasure.model.BankModel;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SupportBankActivity extends BaseActivity {

    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.lv_bank)
    ListView lvBank;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_support_bank);
        ButterKnife.inject(this);
        initView();
        getSupportBankList();
    }


    private void initView() {
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText("选择银行");
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getSupportBankList() {
        ActionUtil actionUtil = new ActionUtil(SupportBankActivity.this);
        actionUtil.getSupportBankList();
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                BankModel bankModel = (BankModel) model;
                if (bankModel == null) {
                    UIHelper.showToast(SupportBankActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (bankModel.getErrorMsg() != null) {
                    UIHelper.showToast(SupportBankActivity.this, bankModel.getErrorMsg());
                } else {
                    List<BankInfo> bankList = bankModel.getPayLimitVMList();
                    BankListAdapter bankListAdapter = new BankListAdapter(SupportBankActivity.this, bankList);
                    lvBank.setAdapter(bankListAdapter);
                    bankListAdapter.setOnItemClickListener(new BankListAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BankInfo info) {
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("bankInfo", info);
                            intent.putExtras(bundle);
                            setResult(RESULT_OK, intent);
                            SupportBankActivity.this.finish();
                        }
                    });
                }
            }
        });
    }


}
